package com.hitqz.robot.biz.flow.config;

import cn.hutool.core.io.FileUtil;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author wengyongxin
 * @date 2023/11/21 00:22
 **/
@Component
@Slf4j
public class AutoLoadRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String folderPath = System.getProperty("user.dir") + File.separator + "file";
        log.info("flowderPahth: " + folderPath);
        File folder = new File(folderPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".txt")) {
                    String fileName = file.getName();
                    fileName = fileName.substring(0, fileName.lastIndexOf(".txt"));
                    String el = FileUtil.readUtf8String(file);
                    //载入流程
                    if (LiteFlowChainELBuilder.validate(el)){
                        LiteFlowChainELBuilder.createChain().setChainId(fileName).setEL(el).build();
                        log.info("[liteFlow]load flow success\nchainId:{}\nel:{}",fileName,el);
                    }
                }
            }
        }
    }
}
